//
//  RootViewController.m
//  Interface
//
//  Created by Letme on 15-4-9.
//  Copyright (c) 2015年 zhenjiea. All rights reserved.
//

#import "HomePageViewController.h"

@interface HomePageViewController ()

@end

@implementation HomePageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    //欧洲国家6个国家 图片/名字
    NSString *url_string = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/index_places/4/"];
#warning
    self.navigationController.navigationBar.translucent = NO;
    
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
        
        //NSLog(@"使用AFN进行get请求 ===  %@",responseObject);
        
      
        self.allModelArray = [NSMutableArray array];
        NSMutableDictionary *dic = responseObject;
        NSMutableArray *array = [dic objectForKey:@"data"];
        for (NSDictionary *smalldic in array) {
            HomePageModel *model = [[HomePageModel alloc] init];
            model.name = [smalldic objectForKey:@"name"];
            model.imageUrl = [smalldic objectForKey:@"cover_route_map_cover"];
            [self.allModelArray addObject:model];
            
            //NSLog(@"所有欧洲国家一级页面:%@", self.allModelArray);
            
            
            [[DataBaseHandle shareDataBaseHandel]openDB];
            [[DataBaseHandle shareDataBaseHandel]createForegin];
            NSMutableArray *array = [[DataBaseHandle shareDataBaseHandel]selectAllEuropName];
            if (![array containsObject:model.name]) {
                //[self.allModelArray addObject:model];
                [[DataBaseHandle shareDataBaseHandel]insertForegin:model];
            }
            //[[DataBaseHandle shareDataBaseHandel]dropAllErop];
            //[self.allModelArray addObject:model];
        }
        
        [self.myCollectionView reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"失败==== %@",error);
        self.allModelArray = [NSMutableArray array];
        [[DataBaseHandle shareDataBaseHandel]openDB];
        self.allModelArray = [[DataBaseHandle shareDataBaseHandel]selectAllForeginModel];
        [self.myCollectionView reloadData];
    }];
    
    
    UICollectionViewFlowLayout *flowlayout = [[UICollectionViewFlowLayout alloc] init];
    flowlayout.itemSize = CGSizeMake((self.view.frame.size.width - 70) / 2, (self.view.frame.size.width - 70) / 2);
    flowlayout.sectionInset = UIEdgeInsetsMake(30, 30, 30, 30);
    //flowlayout.scrollDirection = UICollectionViewScrollDirectionVertical;
#warning 设置头
    //flowlayout.headerReferenceSize = CGSizeMake(0, 50);
    
    self.myCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 15 + (self.view.frame.size.height) / 15, self.view.frame.size.width, self.view.frame.size.height - 150) collectionViewLayout:flowlayout];
    [self.view addSubview:self.myCollectionView];
    //self.myCollectionView.bounces = NO;
    [self.myCollectionView release];
    self.myCollectionView.backgroundColor = [UIColor whiteColor];
    self.myCollectionView.delegate = self;
    self.myCollectionView.dataSource = self;
    
#warning 设置
    [self.myCollectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.allModelArray.count;
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HomePageModel *model = [self.allModelArray objectAtIndex:indexPath.row];
    
    MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    //cell.backgroundColor = [UIColor lightGrayColor];
    cell.model = model;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    //法国
    if (indexPath.row == 0) {
        NSMutableArray *cityArray = [NSMutableArray arrayWithObjects:@"巴黎", nil];
        NSString *url1 = @"http://api.breadtrip.com/destination/place/3/599/pois/all/?start=0&count=20&sort=default&shift=false&latitude=38.88266956321907&longitude=121.53966515009307";
        NSMutableArray *urlArray = [NSMutableArray arrayWithObjects:url1, nil];
        SecondViewController *secondVC = [[SecondViewController alloc] init];
        secondVC.urlArray = urlArray;
        secondVC.cityArray = cityArray;
        [self.navigationController pushViewController:secondVC animated:YES];
    }
    
    //西班牙
    if (indexPath.row == 1) {
        NSMutableArray *cityArray = [NSMutableArray arrayWithObjects:@"马德里",@"巴塞罗那", nil];
        
        NSString *url1 = @"http://api.breadtrip.com/destination/place/3/24059/pois/all/?start=0&count=20&sort=default&shift=false&latitude=38.88265156297574&longitude=121.53968615009308";
        NSString *url2 = @"http://api.breadtrip.com/destination/place/3/81/pois/sights/?start=0&count=20&sort=default&shift=false&latitude=38.88265156297574&longitude=121.53968615009308";
        NSMutableArray *urlArray = [NSMutableArray arrayWithObjects:url1, url2, nil];
        
        SecondViewController *secondVC = [[SecondViewController alloc] init];
        secondVC.urlArray = urlArray;
        //传数组
        secondVC.cityArray = cityArray;
        [self.navigationController pushViewController:secondVC animated:YES];
    }
    //意大利
    if (indexPath.row == 2) {
        NSMutableArray *cityArray = [NSMutableArray arrayWithObjects:@"罗马", @"米兰", @"佛罗伦萨", @"威尼斯", nil];
        NSString *url1 = @"http://api.breadtrip.com/destination/place/3/668/pois/all/?start=0&count=20&sort=default&shift=false&latitude=38.88019752980392&longitude=121.5415241500931";
        NSString *url2 = @"http://api.breadtrip.com/destination/place/3/647/pois/all/?start=0&count=20&sort=default&shift=false&latitude=38.88019752980392&longitude=121.5415241500931";
        NSString *url3 = @"http://api.breadtrip.com/destination/place/3/667/pois/sights/?start=0&count=20&sort=default&shift=false&latitude=38.88019752980392&longitude=121.5415241500931";
        NSString *url4 = @"http://api.breadtrip.com/destination/place/3/666/pois/sights/?start=0&count=20&sort=default&shift=false&latitude=38.88019752980392&longitude=121.5415241500931";
        NSMutableArray *urlArray = [NSMutableArray arrayWithObjects:url1, url2, url3, url4, nil];
        SecondViewController *secondVC = [[SecondViewController alloc] init];
        secondVC.urlArray = urlArray;
        secondVC.cityArray = cityArray;
        [self.navigationController pushViewController:secondVC animated:YES];
        
    }
    //英国
    if (indexPath.row == 3) {
        NSMutableArray *cityArray = [NSMutableArray arrayWithObjects:@"伦敦", @"爱丁堡", @"剑桥", nil];
        NSString *url1 = @"http://api.breadtrip.com/destination/place/3/28470/pois/sights/?start=0&count=20&sort=default&shift=false&latitude=38.882638562800004&longitude=121.53973415009307";
        NSString *url2 = @"http://api.breadtrip.com/destination/place/3/1640/pois/all/?start=0&count=20&sort=default&shift=false&latitude=38.882638562800004&longitude=121.53973415009307";
        NSString *url3 = @"http://api.breadtrip.com/destination/place/3/2274/pois/sights/?start=0&count=20&sort=default&shift=false&latitude=38.882638562800004&longitude=121.53973415009307";
        // NSString *url4 = @"http://api.breadtrip.com/destination/place/3/1367/pois/all/?start=0&count=20&sort=default&shift=false&latitude=38.882638562800004&longitude=121.53973415009307";
        NSMutableArray *urlArray = [NSMutableArray arrayWithObjects:url1, url2, url3, nil];
        SecondViewController *secondVC = [[SecondViewController alloc] init];
        secondVC.urlArray = urlArray;
        secondVC.cityArray = cityArray;
        [self.navigationController pushViewController:secondVC animated:YES];
        
    }
    //德国
    if (indexPath.row == 4) {
        NSMutableArray *cityArray = [NSMutableArray arrayWithObjects:@"柏林", @"慕尼黑", nil];
        NSString *url1 = @"http://api.breadtrip.com/destination/place/3/690/pois/all/?start=0&count=20&sort=default&shift=false&latitude=38.882638562800004&longitude=121.53973415009307";
        NSString *url2 = @"http://api.breadtrip.com/destination/place/3/465/pois/sights/?start=0&count=20&sort=default&shift=false&latitude=38.882638562800004&longitude=121.53973415009307";
        NSMutableArray *urlArray = [NSMutableArray arrayWithObjects:url1, url2, nil];
        SecondViewController *secondVC = [[SecondViewController alloc] init];
        secondVC.urlArray = urlArray;
        secondVC.cityArray = cityArray;
        [self.navigationController pushViewController:secondVC animated:YES];
        
    }
    //奥地利
    
    if (indexPath.row == 5) {
        NSMutableArray *cityArray = [NSMutableArray arrayWithObjects:@"维也纳", nil];
        NSString *url1 = @"http://api.breadtrip.com/destination/place/3/212/pois/all/?start=0&count=20&sort=default&shift=false&latitude=38.882638562800004&longitude=121.53973415009307";
        NSMutableArray *urlArray = [NSMutableArray arrayWithObjects:url1, nil];
        SecondViewController *secondVC = [[SecondViewController alloc] init];
        secondVC.urlArray = urlArray;
        secondVC.cityArray = cityArray;
        [self.navigationController pushViewController:secondVC animated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
